<?php

namespace app\api\controller\market;

use app\api\BaseController;
use app\api\service\ComboService;
use app\api\service\OrderService;
use app\api\service\PaymentService;
use think\exception\ValidateException;

class ComboController extends BaseController
{

    public function index()
    {
        $store_id = $this->request->param('store_id/d');

        if (empty($store_id)){
            return json(['code' =>0, 'msg' => '非法参数!']);
        }

       $comboList =  ComboService::getComboList($store_id);


        return  json(['code' =>1, 'msg' => $comboList]);
    }

    public function buy()
    {
        $param = $this->request->param();

        try {
            $this->validate($param,[
                'combo_id' => 'require|integer',
                'payment_type' => 'require|in:1,2'
            ],[
                'combo_id.require' => '非法请求!',
                'payment_type.require' => '非法请求!',
            ]);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $combo = ComboService::findComboDetail($param['combo_id']);


        if (empty($combo)) {
            return json(['code' => 0, 'msg' => '套餐已下线!']);
        }

        $user_id = $this->request->user['user_id'];
        $total_amount = number_format($combo['sell_price'],2);
        $order_no = PaymentService::generateOrderNum();
        $out_trade_no = PaymentService::generateOrderNum(10);

        $data = [
            'payment_type' => $param['payment_type'],
            'user_id' => $user_id,
            'store_id' => $combo['store_id'],
            'goods_name' => $combo['combo_name'],
            'goods_id' => $combo['id'],
            'image' => $combo['combo_image'],
            'market_price' => $combo['origin_price'],
            'sell_price' => $combo['sell_price'],
            'buy_num' => 1,
            'order_type' => 5,
            'real_amount' => $total_amount,
            'order_no' => $order_no,
            'out_trade_no' => $out_trade_no
        ];

        $result = OrderService::saveOrder($data);

        if ($result === false){
            return  json(['code' =>0, 'msg'=> '下单失败!']);
        }


        $config = PaymentService::findPaymentConfig($data['store_id'],$data['payment_type']);

        if (empty($config)){
            return  json(['code' =>0, 'msg' =>'支付异常!']);
        }


        $payment_data = [
            'config' => $config,
            'trade_method' => $data['payment_type'],
            'out_trade_no' => $out_trade_no,
            'real_amount' => $total_amount,
        ];

        if ($data['payment_type'] == 1){
            $payment_data['notify_url'] = 'https://xiangyule.jcyqkj.cn/api/order/trade/wx/notify';
        }

        if ($data['payment_type'] == 2){
            $payment_data['notify_url'] = 'https://xiangyule.jcyqkj.cn/api/order/trade/ali/notify';
        }


        $prepay_info = PaymentService::createOrder($payment_data);

        if (!empty($prepay_info)){
            return json([
                'code' =>1,
                'data' => [
                    'out_trade_no' => $out_trade_no,
                    'prepay_info' => $prepay_info
                ],
                'msg' => '下单成功!'
            ]);
        }

        return  json(['code' =>0, 'msg' => '下单失败!']);



    }
}